package com.utils.email;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService{

    private final String from = "2216641240@qq.com";

    @Resource
    private JavaMailSender javaMailSender;
    @Override
    public void sendMail(Email email) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(email.getSendTo());
            mimeMessageHelper.setSubject(email.getSubject());
            mimeMessageHelper.setText(email.getContent(),true);
            javaMailSender.send(mimeMessage);

        } catch (MessagingException e) {
            log.error("send mail error , the mail subject if -> {}",email.getSubject(),e);
            throw new RuntimeException(e);
        }
    }
}
